package com.yc.shopping.ui;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.yc.shopping.dao.GoodTypeDao;
import com.yc.shopping.dao.GoodsDao;
import com.yc.shopping.util.ShoppingUtil;
import org.eclipse.swt.widgets.Table;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class Dialog_FindGoods extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	private Combo combo;
	GoodTypeDao typeDao = new GoodTypeDao();
	GoodsDao dao = new GoodsDao();
	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public Dialog_FindGoods(Shell parent, int style) {
		super(parent, style);
		setText("商品购买:");
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(759, 412);
		shell.setText(getText());
		ShoppingUtil.centerWindows(shell);

		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		
		table.setBounds(27, 68, 708, 291);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		TableColumn tableColumn = new TableColumn(table, SWT.NONE);
		tableColumn.setWidth(100);
		tableColumn.setText("商品编号");

		TableColumn tableColumn_1 = new TableColumn(table, SWT.NONE);
		tableColumn_1.setWidth(126);
		tableColumn_1.setText("条形码");

		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(77);
		tblclmnNewColumn.setText("商品名称");

		TableColumn tableColumn_2 = new TableColumn(table, SWT.NONE);
		tableColumn_2.setWidth(69);
		tableColumn_2.setText("类型名称");

		TableColumn tableColumn_3 = new TableColumn(table, SWT.NONE);
		tableColumn_3.setWidth(50);
		tableColumn_3.setText("单位");

		TableColumn tableColumn_4 = new TableColumn(table, SWT.NONE);
		tableColumn_4.setWidth(56);
		tableColumn_4.setText("净重");

		TableColumn tableColumn_5 = new TableColumn(table, SWT.NONE);
		tableColumn_5.setWidth(66);
		tableColumn_5.setText("保质期");

		TableColumn tableColumn_6 = new TableColumn(table, SWT.NONE);
		tableColumn_6.setWidth(50);
		tableColumn_6.setText("进价");

		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(50);
		tblclmnNewColumn_1.setText("售价");

		TableColumn tableColumn_7 = new TableColumn(table, SWT.NONE);
		tableColumn_7.setWidth(59);
		tableColumn_7.setText("库存");

		Label label = new Label(shell, SWT.NONE);
		label.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 10, SWT.NORMAL));
		label.setBounds(27, 35, 61, 17);
		label.setText("商品类型：");

		combo = new Combo(shell, SWT.NONE);
		combo.setBounds(94, 34, 158, 25);

		Button btn_ffind = new Button(shell, SWT.NONE);
		btn_ffind.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// 查询
				showGoods();
			}
		});
		btn_ffind.setBounds(287, 35, 80, 27);
		btn_ffind.setText("查询");
		//当所有组件加载完成后,加载数据
		showTypes();
		showGoods();
	}
	public void showGoods() {
		//从下拉列表中获取数据
		String tname= combo.getText().trim();
		System.out.println("tname="+tname);
		List<Map<String, Object>> list = null;
		
		try {
			if("全部".equals(tname)) {
				list=dao.findAll();
			}else {
				list=dao.findByTname(tname);	
			}
			if(null==list||list.isEmpty()) {
				ShoppingUtil.showMessage(shell, "消息提示", "暂无商品数据");
				return;
			}
			
			//清空表格中原有的数据
			table.removeAll();
			TableItem tableItem =null;
			for (Map<String, Object> map : list) {
				tableItem = new TableItem(table,SWT.NONE);
				tableItem.setText(new String[] {
						ShoppingUtil.objToString(map.get("GID")),
						ShoppingUtil.objToString(map.get("GCODE")),
						ShoppingUtil.objToString(map.get("GNAME")),
						ShoppingUtil.objToString(map.get("TNAME")),
						ShoppingUtil.objToString(map.get("UNIT")),
						ShoppingUtil.objToString(map.get("WEIGHT")),
						ShoppingUtil.objToString(map.get("GDATE")),
						ShoppingUtil.objToString(map.get("PRICE")),
						ShoppingUtil.objToString(map.get("SALEPRICE")),
						ShoppingUtil.objToString(map.get("BALANCE")),
				});
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	private void showTypes() {
		try {
			List<Map<String,Object>> list = typeDao.findAll();
			
			String []  infos= new String[list.size()+1];
			infos[0] = "全部";
			for (int i = 0; i <list.size(); i++) {
				Map<String, Object> m = list.get(i);
				
				infos[i+1] = ShoppingUtil.objToString(m.get("TNAME"));
				
			}		
			//设置到下拉列表中
			combo.removeAll();//清楚下拉列表中数据
			combo.setItems(infos);
			combo.select(0);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
}
